package com.wyg.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.wyg.pojo.Question;
import com.wyg.util.HibernateSessionFactory;

public class QuestionDao {

	//精选试题
	public List showPartQuestion(){
		Session session = HibernateSessionFactory.getSession();
		Transaction trans = session.beginTransaction();
		
		String hql = "from Question q where q.id between 1 and 5";
		Query query = session.createQuery(hql);
		
		List list = query.list();
		
		trans.commit();
		return list;
	}
	
	//全部选择
	public List showAllChoiceQuestion(){
		Session session = HibernateSessionFactory.getSession();
		Transaction trans=session.beginTransaction();
		
		String hql = "from Question q where q.type ='选择'";
		Query query = session.createQuery(hql);
		
		List list = query.list();
		trans.commit();
		System.out.println("QuestionDao--"+list.size());
		return list;
	}
	//全部判断
	public List showAllJudgeQuestion(){
		Session session = HibernateSessionFactory.getSession();
		Transaction trans = session.beginTransaction();
		
		String hql = "from Question q where q.type='判断'";
		Query query = session.createQuery(hql);
		
		List list = query.list();
		trans.commit();
		return list;
	}
	//显示一道题目信息
	public Question showOneQuestion(int id){
		Session session = HibernateSessionFactory.getSession();
		Transaction t = session.beginTransaction();
		
		Question q = (Question) session.get(Question.class, id);
		t.commit();
		return q;
	}
	//修改选择
	public void changeChiceQuestion(int id,String title,String optionA,String optionB,String optionC,String optionD,String answer){
		Session session = HibernateSessionFactory.getSession();
		Transaction t = session.beginTransaction();
		
		Question q = (Question) session.get(Question.class, id);
		q.setTitle(title);
		q.setOptionA(optionA);
		q.setOptionB(optionB);
		q.setOptionC(optionC);
		q.setOptionD(optionD);
		q.setAnswer(answer);
		
		t.commit();
	}
	
	
}
